package com.cq.xjms.web.actions.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cq.xjms.model.Clazz;
import com.cq.xjms.model.Course;
import com.cq.xjms.model.Department;
import com.cq.xjms.model.Institute;
import com.cq.xjms.model.Score;
import com.cq.xjms.model.Student;
import com.cq.xjms.service.ScoreManager;
import com.cq.xjms.service.StudentManager;
import com.cq.xjms.service.SysManager;
import com.cq.xjms.util.Constants;
import com.cq.xjms.web.actions.BaseAction;
@SuppressWarnings("unchecked")
public class StuAction extends BaseAction{
	private static Logger log = Logger.getLogger(StuAction.class);
	private static final long serialVersionUID = -879633312645153566L;
	private String type;
	private Student student;
	private List<Student> students;
	private Map<String,Object> params = new HashMap<String,Object>();
	private StudentManager studentManager;
	private SysManager sysManager;
	private ScoreManager scoreManager;
	private List<Institute> institutes;
	private Department department;
	private Institute institute;
	private List<Clazz> clazzs;
	private List<Score> scores;
	private Clazz clazz;
	private Course course;
	private String courseId;
	private String newpassword;
	private String surepassword;
	private String scoreyear;
	private String scoreterm;
	private double allCredit;
	private double reCredit;
	private double allAvgPoint;
	private double aveAvgPoint;
	
	public String getScoreyear() {
		return scoreyear;
	}
	public void setScoreyear(String scoreyear) {
		this.scoreyear = scoreyear;
	}
	public String getScoreterm() {
		return scoreterm;
	}
	public void setScoreterm(String scoreterm) {
		this.scoreterm = scoreterm;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	public String getSurepassword() {
		return surepassword;
	}
	public void setSurepassword(String surepassword) {
		this.surepassword = surepassword;
	}
	public StudentManager getStudentManager() {
		return studentManager;
	}
	public SysManager getSysManager() {
		return sysManager;
	}
	public String add(){
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	public String index(){
		student = (Student)getSession().getAttribute(Constants.LOGIN_STUDENT);
		if(student== null){
			return INPUT;
		}
		return SUCCESS;
	}
	//学生登录
	public String login(){
		
		if(StringUtils.isNotEmpty(student.getStuNo()) && StringUtils.isNotEmpty(student.getPassword())){
			student = (Student)sysManager.login(student.getStuNo(), student.getPassword(),3);
		}else{
			student = null;
		}
		if(student == null){
			List list = new ArrayList();
			list.add("账号或密码错误!");
			this.setActionErrors(list);
			return INPUT;
		}else{
			getSession().setAttribute(Constants.LOGIN_STUDENT, student);
		}
		return SUCCESS;
	}
	//退出系统�
	public String logout(){
		getSession().invalidate();
		return SUCCESS;
	}
	
	public String left(){
		return SUCCESS;
	}
	
	public String line(){
		return SUCCESS;
	}
	public String save(){
		try{
			params.put("stuNo",student.getStuNo());
			students = sysManager.find(Student.STUDENT_QUERY,params);
			if(students==null || students.size()==0){
				if(student.getClazz().getId()==null){
					student.setClazz(null);
				}
				 sysManager.saveOrUpdate(student);
				 type="add";
			}else{
				 if(student.getId()!=null&&student.getId().equals(students.get(0).getId())){
					 sysManager.saveOrUpdate(student);
					 type="edit";
				 }else{
					 List list = new ArrayList();
					 list.add("此学号已经存在！");
					 this.setActionErrors(list);
				 }
			}
		}catch(Exception e){
			log.error("save student failed",e);
			return INPUT;
		}
		student = null;
		return SUCCESS;
	}
	
	public String queryStudent(){
		try{
			student = (Student)getSession().getAttribute(Constants.LOGIN_STUDENT);
			if(student!=null){
				params.put("id", student.getId());
				students = studentManager.find(Student.STUDENT_QUERYBYID,params);
				if(students!=null && students.size()>0){
					student = students.get(0);
				}
			}
		}catch(Exception e){
			log.error("",e);
		}
		return SUCCESS;
	}
	
	public String updatePassword(){
		
		return SUCCESS;
	}
	
	public String savePassword(){
	  try{
		String oldpassword=student.getPassword();
		student = (Student)getSession().getAttribute(Constants.LOGIN_STUDENT);
		params.put("stuNo", student.getStuNo());
		students=studentManager.find(Student.STUDENT_QUERY, params);
		student=students.get(0);
		if(oldpassword.equals(student.getPassword())){
			if(newpassword.equals(surepassword)){
				student.setPassword(newpassword);
				sysManager.saveOrUpdate(student);
				type="update";
			}else{
				List list = new ArrayList();
				 list.add("输入密码不一致");
				 this.setActionErrors(list);
			}
		}else{
			List list = new ArrayList();
			 list.add("输入原密码不正确");
			 this.setActionErrors(list);
		}
		
	  }catch(Exception e){
		  log.error("",e);
	  }
		return SUCCESS;
	}
	
	
	public String queryStudentScore(){
		 String yearterm="";
		 if(scoreyear !=null){
			 yearterm +=scoreyear;
		 }
		 if(scoreterm !=null){
			 yearterm +=scoreterm;
		 }
		try{
		   student=(Student)getSession().getAttribute(Constants.LOGIN_STUDENT);
		   scores = scoreManager.queryScoreByStudent(student.getId(),yearterm, getLimit());
		   totalRows = scoreManager.countScoreByStudent(student.getId(),yearterm);
	       for(Score s:scores){
	    	   allCredit +=s.getCourse().getCredit();
	    	   allAvgPoint +=(s.getCourse().getCredit())*(s.getAvgPoint());
	    	   if((s.getPoint() >=60)&&(s.getMark()==1)){
	    		   reCredit +=s.getCourse().getCredit();
	    	   }
	    	   
	       }
	       aveAvgPoint=allAvgPoint/allCredit;
		}catch(Exception e){
			log.error("",e);
		}
	     return SUCCESS;	
	}
	
	
	public String noPassCourse(){
		try{
			   student=(Student)getSession().getAttribute(Constants.LOGIN_STUDENT);
			   scores = scoreManager.queryCourseNoPassByStudent(student.getId(), getLimit());
			   totalRows = scoreManager.countCourseNoPassByStudent(student.getId());
		
			}catch(Exception e){
				log.error("",e);
			}
		     return SUCCESS;	
	}
	
	
	public String queryStudentCourse(){
		 String yearterm="";
		 if(scoreyear !=null){
			 yearterm +=scoreyear;
		 }
		 if(scoreterm !=null){
			 yearterm +=scoreterm;
		 }
		 
		try{
		   student=(Student)getSession().getAttribute(Constants.LOGIN_STUDENT);
		   scores = scoreManager.queryCourseByStudent(student.getId(),yearterm, getLimit());
		   totalRows = scoreManager.countCourseByStudent(student.getId(),yearterm);
	
		}catch(Exception e){
			log.error("",e);
		}
	     return SUCCESS;	
	}
	
	public String studentChoiceCourse(){
		return SUCCESS;
	}
	
	public String delete(){
		studentManager.remove(Student.class, student.getId());
		return SUCCESS;
	}
	public String list(){
		if(student==null){
			student = new Student();
			clazz = new Clazz();
			student.setClazz(clazz);
		}
		students = studentManager.queryStudent(student.getName(), student.getStuNo(), student.getIdcard(), student.getClazz().getId(), getLimit());
		totalRows = studentManager.countStudent(student.getName(), student.getStuNo(), student.getIdcard(), student.getClazz().getId());
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		
		return SUCCESS;
	}
	
	public String edit(){
		try{
			params.put("id", student.getId());
			students = studentManager.find(Student.STUDENT_QUERYBYID,params);
			if(students!=null && students.size()>0){
				student = students.get(0);
			}
		}catch(Exception e){
			log.error("",e);
		}
		return SUCCESS;
	}
	
	public String getStudentByClazzId(){
		if(clazz!=null){
			students = studentManager.queryStudent(null,null,null, clazz.getId(), null);
		}
		JSONArray arr = new JSONArray();
		for(Student s :students){
			JSONObject obj = new JSONObject();
			obj.put("id", s.getId());
			obj.put("name", s.getName());
			arr.add(obj);
		}
		writeJSON(arr.toString());
		return NONE;
	}
	public String elective(){ //选修
         
		return SUCCESS;
	}

	public String getType() {
		return type;
	}
	

	public void setType(String type) {
		this.type = type;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public List<Student> getStudents() {
		return students;
	}
	public void setStudents(List<Student> students) {
		this.students = students;
	}

	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}

	public List<Institute> getInstitutes() {
		return institutes;
	}

	public void setInstitutes(List<Institute> institutes) {
		this.institutes = institutes;
	}

	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Institute getInstitute() {
		return institute;
	}

	public void setInstitute(Institute institute) {
		this.institute = institute;
	}

	public List<Clazz> getClazzs() {
		return clazzs;
	}

	public void setClazzs(List<Clazz> clazzs) {
		this.clazzs = clazzs;
	}

	public Clazz getClazz() {
		return clazz;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public String getCourseId() {
		return courseId;
	}

	public void setCourseId(String courseId) {
		this.courseId = courseId;
	}
	public ScoreManager getScoreManager() {
		return scoreManager;
	}
	public void setScoreManager(ScoreManager scoreManager) {
		this.scoreManager = scoreManager;
	}
	public List<Score> getScores() {
		return scores;
	}
	public void setScores(List<Score> scores) {
		this.scores = scores;
	}
	public double getAllCredit() {
		return allCredit;
	}
	public void setAllCredit(double allCredit) {
		this.allCredit = allCredit;
	}
	public double getAllAvgPoint() {
		return allAvgPoint;
	}
	public void setAllAvgPoint(double allAvgPoint) {
		this.allAvgPoint = allAvgPoint;
	}
	public double getAveAvgPoint() {
		return aveAvgPoint;
	}
	public void setAveAvgPoint(double aveAvgPoint) {
		this.aveAvgPoint = aveAvgPoint;
	}
	public double getReCredit() {
		return reCredit;
	}
	public void setReCredit(double reCredit) {
		this.reCredit = reCredit;
	}
	
	
	
	
}
